package com.htsk.city.services.admin.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.htsk.city.services.admin.aspect.LoginUserAspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @Author：mjiaxing
 * @CreateTime：2023-04-10
 * @Description：
 * @Version：1.0.0
 */

@Configuration
public class MVCConfig implements WebMvcConfigurer {
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${application.file.path}")
    private String localFilePath;

    /**
     * 资源映射路径 前缀
     */
    @Value("${application.file.prefix}")
    public String localFilePrefix;


    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        registry.addResourceHandler(localFilePrefix + "/**")
                .addResourceLocations("file:" + localFilePath + File.separator);
    }

    @Resource
    LoginUserAspect loginUserAspect;
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){
        argumentResolvers.add(loginUserAspect);
    }
    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，定义详细认证规则
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 登录验证
            SaRouter.match("/**").notMatch("/user/login","/user/permission","/statics/**").check(r -> {});
        })).addPathPatterns("/**");
    }

}
